#if defined(DUET2)||defined(TSINGMA)
#if (FEATURE_MODE == 0)
/**
 @file sys_duet2_oam.c


 @author  Copyright (C) 2020 Centec Networks Inc.  All rights reserved.

 @date 2020-03-17

 @version v1.0

*/

/****************************************************************************
 *
* Header Files
*
****************************************************************************/
#include "ctc_oam.h"
#include "ctc_error.h"
#include "sys_usw_oam_db.h"

extern sys_oam_master_t* g_oam_master[CTC_MAX_LOCAL_CHIP_NUM_PP];
int32
sys_dute2_oam_get_mep_info_for_event(uint8 lchip, void* p_mep_info)
{
    uint32 lmep_index   = 0;
    uint32 cmd  = 0;
    ctc_oam_mep_type_t mep_type = 0;
    DsEthMep_m   ds_mep;
    DsEthMep_m   ds_rmep;   
    ds_eth_mep_d2_t* ds_eth_mep = NULL;
    ds_eth_rmep_d2_t* ds_eth_rmep = NULL;
    ds_bfd_mep_d2_t* ds_bfd_mep = NULL;
    ds_bfd_rmep_d2_t* ds_bfd_rmep = NULL;    
    sys_oam_lmep_t* p_sys_lmep_com  = NULL;
    sys_oam_rmep_t* p_sys_rmep_com  = NULL;
    ctc_oam_y1731_lmep_info_t*  p_lmep_y1731_info = NULL;
    ctc_oam_y1731_rmep_info_t*  p_rmep_y1731_info = NULL;
    ctc_oam_bfd_lmep_info_t*    p_lmep_bfd_info = NULL;
    ctc_oam_bfd_rmep_info_t*    p_rmep_bfd_info = NULL;
    ctc_oam_mep_info_t* mep_info = p_mep_info;

    cmd    = DRV_IOR(DsEthRmep_t,  DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, mep_info->mep_index, cmd, &ds_rmep));

    ds_eth_mep = (ds_eth_mep_d2_t*)(&ds_mep);
    ds_eth_rmep = (ds_eth_rmep_d2_t*)(&ds_rmep);
    ds_bfd_mep = (ds_bfd_mep_d2_t*)(&ds_mep);
    ds_bfd_rmep = (ds_bfd_rmep_d2_t*)(&ds_rmep);
    mep_info->is_rmep = ds_eth_rmep->is_remote;
    if (!(ds_eth_rmep->active))
    {
        return CTC_E_NOT_READY;
    }

    if (mep_info->is_rmep == 1)
    {
        p_sys_rmep_com = ctc_vector_get(g_oam_master[lchip]->mep_vec, mep_info->mep_index);
        if (p_sys_rmep_com)
        {
            mep_type = p_sys_rmep_com->p_sys_lmep->p_sys_chan->mep_type;
        }
        else
        {
            return CTC_E_INVALID_PARAM;
        }

        if ((CTC_OAM_MEP_TYPE_ETH_1AG == mep_type)
            || (CTC_OAM_MEP_TYPE_ETH_Y1731 == mep_type)
            || (CTC_OAM_MEP_TYPE_MPLS_TP_Y1731 == mep_type))
        {
            p_rmep_y1731_info = &(mep_info->rmep.y1731_rmep);
            p_rmep_y1731_info->active                  = 1;
            p_rmep_y1731_info->first_pkt_rx        = ds_eth_rmep->first_pkt_rx;
            p_rmep_y1731_info->d_loc                   = ds_eth_rmep->d_loc;
            p_rmep_y1731_info->d_unexp_period   = ds_eth_rmep->d_unexp_period;
            p_rmep_y1731_info->ma_sa_mismatch   = ds_eth_rmep->rmepmacmismatch;
            p_rmep_y1731_info->mac_status_change = ds_eth_rmep->mac_status_change;
            p_rmep_y1731_info->last_rdi         = ds_eth_rmep->rmep_last_rdi;
            p_rmep_y1731_info->is_p2p_mode = ds_eth_rmep->eth_oam_p2_p_mode;
            if (p_rmep_y1731_info->is_p2p_mode && ds_eth_rmep->is_csf_en)
            {
                p_rmep_y1731_info->d_csf        = (ds_eth_rmep->u1_g1_ccm_seq_num_31_10 >>19)&0x1;
            }
            p_rmep_y1731_info->lmep_index = p_rmep_y1731_info->is_p2p_mode ? (mep_info->mep_index - 1) : ds_eth_rmep->u3_g1_mep_index;
            lmep_index = p_rmep_y1731_info->lmep_index;
        }
        else
        {
            p_rmep_bfd_info = &(mep_info->rmep.bfd_rmep);
            p_rmep_bfd_info->first_pkt_rx = ds_bfd_rmep->first_pkt_rx;
            p_rmep_bfd_info->mep_en = ds_bfd_rmep->active;
            if (CTC_OAM_MEP_TYPE_MPLS_TP_BFD == mep_type)
            {
                p_rmep_bfd_info->mis_connect        = CTC_IS_BIT_SET(ds_bfd_rmep->u1_g1_ip_sa_22_0, 0);
            }
            p_rmep_bfd_info->remote_diag = ds_bfd_rmep->remote_diag;
            p_rmep_bfd_info->remote_state = ds_bfd_rmep->remote_stat;
            p_rmep_bfd_info->lmep_index  = (mep_info->mep_index - 1);          
            lmep_index = p_rmep_bfd_info->lmep_index;
        }
    }
    else
    {
        lmep_index = mep_info->mep_index;
    }

    if(mep_info->is_rmep)
    {
        cmd     = DRV_IOR(DsEthMep_t,   DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, lmep_index, cmd, &ds_mep));
    }
    else
    {
        sal_memcpy(&ds_mep, &ds_rmep, sizeof(DsEthMep_m));
    }

#if (SDK_WORK_PLATFORM == 1)
    if (!(ds_eth_mep->active))
    {
        return CTC_E_NONE;
    }
#endif
    p_sys_lmep_com = (sys_oam_lmep_t*)ctc_vector_get(g_oam_master[lchip]->mep_vec, lmep_index);
    if (p_sys_lmep_com)
    {
        if (p_sys_lmep_com->p_sys_chan)
        {
            mep_type = p_sys_lmep_com->p_sys_chan->mep_type;
        }
        mep_info->mep_type = mep_type;
    }
    else
    {
        return CTC_E_INVALID_PARAM;
    }

    if ((CTC_OAM_MEP_TYPE_ETH_1AG == mep_type)
        || (CTC_OAM_MEP_TYPE_ETH_Y1731 == mep_type)
        || (CTC_OAM_MEP_TYPE_MPLS_TP_Y1731 == mep_type))
    {
        p_lmep_y1731_info = &(mep_info->lmep.y1731_lmep);
        p_lmep_y1731_info->active         = 1;
        p_lmep_y1731_info->d_unexp_mep    = ds_eth_mep->d_unexp_mep;
        p_lmep_y1731_info->d_mismerge     = ds_eth_mep->d_mismerge;
        p_lmep_y1731_info->d_meg_lvl      = ds_eth_mep->d_meg_lvl;
        p_lmep_y1731_info->vlan_id        = ds_eth_mep->mep_primary_vid;
        p_lmep_y1731_info->present_rdi    = ds_eth_mep->present_rdi;
        p_lmep_y1731_info->level = p_sys_lmep_com->md_level;
    }
    else
    {
        p_lmep_bfd_info = &(mep_info->lmep.bfd_lmep);
        p_lmep_bfd_info->mep_en =  ds_bfd_mep->active;
        p_lmep_bfd_info->loacl_state = ds_bfd_mep->local_stat;
        p_lmep_bfd_info->local_diag = ds_bfd_mep->local_diag;
        p_lmep_bfd_info->local_discr = p_sys_lmep_com->local_discr;
        if (CTC_OAM_MEP_TYPE_MPLS_TP_BFD == mep_type)
        {
            if (!mep_info->is_rmep)
            {
                cmd = DRV_IOR(DsBfdRmep_t,  DRV_ENTRY_FLAG);
                CTC_ERROR_RETURN(DRV_IOCTL(lchip, lmep_index + 1, cmd, ds_bfd_rmep));
                mep_info->rmep.bfd_rmep.mis_connect = CTC_IS_BIT_SET(ds_bfd_rmep->u1_g1_ip_sa_22_0, 0);
            }
            else if (ds_bfd_rmep->is_csf_en)
            {
                p_lmep_bfd_info->csf_en       = 1;
                p_lmep_bfd_info->d_csf        = (ds_bfd_mep->u2_g1_bfd_srcport_15_8 >> 4)&0x1;
                p_lmep_bfd_info->rx_csf_type  = (ds_bfd_mep->u2_g1_bfd_srcport_15_8 >> 1)&0x7;
            }
        }
    }

    return CTC_E_NONE;
}

#endif
#endif
